using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.MonitorLizard.Forms
{
	public partial class LizardWorkItem : Form
	{
		private WorkItem workItem;
		private TFServerConnection connection;

		public LizardWorkItem(TFServerConnection connection, WorkItem workItem)
		{
			InitializeComponent();
			this.connection = connection;
			this.workItem = workItem;
			wifcItem.Item = workItem;
			txtItemId.Text = string.Format("{0}:{1}",workItem.Type.Name ,(workItem.Id==0)?"*New*":workItem.Id.ToString());
			
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			Save();
		}

		private void Save()
		{

			System.Collections.ArrayList validationResults = workItem.Validate();
			if (validationResults.Count > 0)
			{
				List<string> errs = new List<string>();
				foreach (Field f in validationResults)
				{
					errs.Add(f.Name);
				}
				string msg = string.Format("Please correct the field(s):\r\n{0}\r\n", string.Join(", ", errs.ToArray()));
				MessageBox.Show(msg, "Invalid Field Values");
			}
			else
			{
				WorkItemStore wis = new WorkItemStore(connection.TeamFoundationServer);
				wis.BatchSave(new WorkItem[] { workItem });
				validationResults = workItem.Validate();
				Close();
			}
		}

		private void tsbSave_Click(object sender, EventArgs e)
		{
			Save();
		}
	}
}